package com.tao.utilslib.data;

public class IntgerByteUtils {


    public static byte[] int2Bytes(int value, int len) {
        byte[] b = new byte[len];
        for (int i = 0; i < len; i++) {
//            b[len - i - 1] = (byte) ((value >> 8 * i) & 0xff);
            b[len - i - 1] = (byte) ((value >> 8 * i));
        }
        return b;
    }


    public static int bytes2Int(byte[] b, int start, int len) {
        int sum = 0;
        int end = start + len;
        for (int i = start; i < end; i++) {
//            int n = ((int)b[i]) & 0xff;
            int n = b[i] & 0xff;

            n <<= (--len) * 8;
            sum += n;
        }
        return sum;
    }


    public static void main(String[] args) {
        int v = 6553536;
        int len = 4;
        byte[] bytes1 = int2Bytes(v, 2);
        System.err.println(StringUtils.bytes2HexString(bytes1));
        int i = bytes2Int(bytes1, 0, bytes1.length);
        System.err.println(i);
    }

}
